package org.jeecg.modules.nc.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.nc.entity.NcMarbasclass;
import org.jeecg.modules.nc.mapper.NcMarbasclassMapper;
import org.jeecg.modules.nc.vo.NcMarbasclassVO;
import org.jeecg.modules.nc.service.INcMarbasclassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: 物料分类表
 * @Author: qixin-boot
 * @Date:   2021-07-06
 * @Version: V1.0
 */
@Service
public class NcMarbasclassServiceImpl extends ServiceImpl<NcMarbasclassMapper, NcMarbasclass> implements INcMarbasclassService {

    @Autowired
    private NcMarbasclassMapper ncMarbasclassMapper;
    @Override
    public List<NcMarbasclassVO> queryList() {
        List<NcMarbasclassVO>  marbasclassVOList = new ArrayList<>();
        List<NcMarbasclassVO>  voList = queryListParentId(null);
        //递归
        voList = getTreeList(voList,marbasclassVOList);
        return voList;
    }

    @Override
    public List<NcMarbasclass> queryTreeList() {
        List<NcMarbasclass>  marbasclassVOList = new ArrayList<>();
        List<NcMarbasclass>  voList = queryTreeListParentId(null);
        //递归
        voList = queryTreeList(voList,marbasclassVOList);
        return voList;
    }

    @Override
    public Integer querycount(NcMarbasclass ncMarbasclass) {
        return ncMarbasclassMapper.querycount(ncMarbasclass);
    }

    @Override
    public List<NcMarbasclass> queryExtList() {
        return ncMarbasclassMapper.queryExtList();
    }

    private List<NcMarbasclass> queryTreeList(List<NcMarbasclass> voList, List<NcMarbasclass> marbasclassVOList) {
        List<NcMarbasclass> list = new ArrayList<>();
        for (NcMarbasclass vo :voList) {
//            vo.setChildren(queryTreeList(queryTreeListParentId(vo.getSid()),marbasclassVOList));
            List<NcMarbasclass>  ncMarbasclassVOS = queryTreeListParentId(vo.getSid());
            if (ncMarbasclassVOS.size()>0){
//                getTreeList(list,marbasclassVOList);
                vo.setChildren(queryTreeList(queryTreeListParentId(vo.getSid()),marbasclassVOList));

            }
            list.add(vo);
        }
        return list;
    }

    private List<NcMarbasclass> queryTreeListParentId(String pid) {
//        QueryWrapper queryWrapper = new QueryWrapper();
//        if(pid == null){
//            queryWrapper.isNull("pk_parent");
//        }else{
//            queryWrapper.eq("pk_parent",pid);
//        }
        List<NcMarbasclass> volist = ncMarbasclassMapper.queryTreeListParentId(pid);
        return volist;
    }

    private List<NcMarbasclassVO> getTreeList(List<NcMarbasclassVO> voList, List<NcMarbasclassVO> marbasclassVOList) {
        List<NcMarbasclassVO> list = new ArrayList<>();
        for (NcMarbasclassVO vo :voList) {
            vo.setChildren(getTreeList(queryListParentId(vo.getValue()),marbasclassVOList));
//            List<NcMarbasclassVO>  list = queryListParentId(vo.getValue());
//            if (list.size()>0){
//                getTreeList(list,marbasclassVOList);
//            }
            list.add(vo);
        }
        return list;
    }

    private List<NcMarbasclassVO> queryListParentId(String pid) {
        List<NcMarbasclassVO> voList = ncMarbasclassMapper.queryListParentId(pid);
        return voList;

    }
}
